﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OnlineManagement.BLL;
namespace OnlineManagement.Web.pages
{
    public partial class HogPurchase : System.Web.UI.Page
    {
        protected bool _isPurchaseWhole=false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack){
                HttpCookie cookie = new HttpCookie(OnlineManagement.Common.PubConstant.HoggeryIdCookieName, "1001");
                Response.Cookies.Add(cookie);
                if (Response.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName) != null)
                {
                    string hoggeryIdStr = Response.Cookies.Get(OnlineManagement.Common.PubConstant.HoggeryIdCookieName).Value;
                    int hoggeryId = 0;
                    if (int.TryParse(hoggeryIdStr, out hoggeryId))
                    {
                        InitPageForHog(hoggeryId);
                    }
                }
            }
            //linkPost.ServerClick += new EventHandler(cbFull_CheckedChanged);
            cbFull.CheckedChanged += new EventHandler(cbFull_CheckedChanged);
            cbScattered.CheckedChanged += new EventHandler(cbScattered_CheckedChanged);
        }

        /// <summary>
        /// 零散
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbScattered_CheckedChanged(object sender, EventArgs e)
        {
            if (cbScattered.Checked)
            {
                cbFull.Checked = false;
                hfIsPurchaseWhole.Value = "false";
            }
            else {
                cbFull.Checked = true;
                hfIsPurchaseWhole.Value = "true";
            }
            DataSet ds = GetHogPurchaseCache();
            if (ds == null)
            {
                BLL.ProcedureHandle handle = new ProcedureHandle();
                ds = handle.GetHogInfoForPurchase(1001);
            }
            if (ds != null && ds.Tables != null)
            {
                ddlPigPen.DataSource = ds.Tables[1];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();
            }
        }

        /// <summary>
        /// 整窝
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cbFull_CheckedChanged(object sender, EventArgs e)
        {

            if (cbFull.Checked)
            {
                cbScattered.Checked = false;
                hfIsPurchaseWhole.Value = "true";
            }
            else {
                cbScattered.Checked = true;
                hfIsPurchaseWhole.Value = "false";
            }

            DataSet ds = GetHogPurchaseCache();
            if(ds==null){
               BLL.ProcedureHandle handle = new ProcedureHandle();
               ds = handle.GetHogInfoForPurchase(1001);
            }
            if (ds != null && ds.Tables != null)
            {
                ddlPigPen.DataSource = ds.Tables[3];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();
            }
        }

        private  DataSet GetHogPurchaseCache()
        {
            string cacheKey = Common.PubConstant.CacheHogPurchaseDataName;
            DataSet ds = Common.DataCache.GetCacheBuisseData(cacheKey);
            return ds;
        }

        private void InitPageForHog(int hoggeryId) {
            try
            {
                DataSet ds = GetHogPurchaseCache();
                if (ds == null)
                {
                    BLL.ProcedureHandle handle = new ProcedureHandle();
                    ds = handle.GetHogInfoForPurchase(hoggeryId);
                    if (ds != null && ds.Tables != null)
                    {
                        string cacheKey = Common.PubConstant.CacheHogPurchaseDataName;
                        Common.DataCache.CacheBuisseData(ds, cacheKey);
                    }
                }
                BindData(ds);

            }
            catch (Exception) { 
                
            }
        }

        private void BindData(DataSet ds)
        {
            if (ds != null && ds.Tables != null)
            {
                ddlHogType.DataSource = ds.Tables[0];
                ddlHogType.DataTextField = "Name";
                ddlHogType.DataValueField = "ID";
                ddlHogType.DataBind();
                //初始化使用合并可用的pigpen
                ddlPigPen.DataSource = ds.Tables[1];
                ddlPigPen.DataTextField = "Name";
                ddlPigPen.DataValueField = "ID";
                ddlPigPen.DataBind();

                ddlGrowPhase.DataSource = ds.Tables[4];
                ddlGrowPhase.DataTextField = "Name";
                ddlGrowPhase.DataValueField = "ID";
                ddlGrowPhase.DataBind();
            }
        }


    }
}